home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / AIFF_DSP_v15 folder / AIFF_DSP / analyze.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-31  |  1.5 KB  |  70 lines  |  [TEXT/KAHL]

  1. /*
  2. FILE:    analyze.c
  3. PROJECT: Ford grant DSP
  4. AUTHOR:  Ben Denckla
  5. COMMENT: Collects statistics about the signal.
  6. */
  7.  
  8. #include "aiff.h"
  9.  
  10. #include <stdio.h>
  11. #include <math.h>
  12. #include <limits.h>
  13. #include <string.h>
  14.  
  15. #define BARMAX 60
  16. #define BINSLOG2 4
  17. #define BINS (1 << BINSLOG2)
  18. #define HSHIFT (16 - BINSLOG2)
  19. #define HMASK ( (BINS - 1) << HSHIFT )
  20.  
  21. #define BINPRINT( ofs ) \
  22.     for (i=ofs + BINS/2 - 1; i>=ofs; i--) { \
  23.         barlen = (float) BARMAX * histogram[i]/histmax; \
  24.         printf( "bin %4d: %8lx %.*s\n", \
  25.                 i - ofs*2, histogram[i], barlen, bar ); \
  26. }
  27.  
  28. int take_input = 1, make_output = 0;
  29.  
  30. static short min, max;
  31. static long histogram[BINS] = {0};
  32. static double sum_of_squares = 0.0;
  33.  
  34. void init_process( void ) {
  35.     if ( (ba.com.wdsi < 9) || (ba.com.wdsi > 16) )
  36.         err( "Cannot process a file with this word size" );
  37. }
  38.  
  39. void term_process ( void ) {
  40.     double rms;
  41.     int i, barlen;
  42.     long histmax = 0;
  43.     char bar[80];
  44.  
  45.     memset( bar, '*', BARMAX );
  46.     rms = sqrt( sum_of_squares / (ba.com.chan * ba.com.fram) );
  47.     for (i=0; i<BINS; i++)
  48.         if ( histogram[i] > histmax ) histmax = histogram[i];
  49.     
  50.     printf( "rms val: %.1lf\nmin: %6hd\nmax: %6hd\n", rms, min, max );
  51.     
  52.     BINPRINT( 0 );
  53.     BINPRINT( BINS/2 );
  54. }
  55.  
  56. void process_samdat ( long buflen ) {
  57.     register short *td, *endd;
  58.     
  59.     td   = d;
  60.     min = max = *td;
  61.     endd = &td[ba.com.chan * buflen];
  62.     
  63.     do {
  64.         if ( *td < min ) min = *td;
  65.         if ( *td > max ) max = *td;
  66.         sum_of_squares += (float) *td * *td;
  67.         histogram[ ((unsigned short ) *td & HMASK) >> HSHIFT ]++;
  68.         td++;
  69.     } while ( td < endd );
  70. }